/**
 * Candidate number: 35834
 */
package tv.starfu.animae;

import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.World;
import com.jme3.app.SimpleApplication;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.terrain.geomipmap.TerrainGrid;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.heightmap.AbstractHeightMap;
import com.jme3.terrain.heightmap.ImageBasedHeightMap;
import com.jme3.texture.Texture;
import tv.starfu.components.Transform;
import tv.starfu.components.level.GridPosition;
import tv.starfu.components.level.KyberneteGrid;
import tv.starfu.components.level.Path;


public class Terrain extends Anima {

    
    private Node systemRoot = new Node("terrainNode");
    private BulletAppState bullet;    
    private Material mat_terrain;
    private TerrainQuad terrainQuad;
    private TerrainGrid terrainGrid;
    private RigidBodyControl terrainPhysics;
    
    private float grassScale = 64;
    private float dirtScale = 16;
    private float rockScale = 128;
    private boolean usePhysics = false;
    private boolean physicsAdded = false;

    private SimpleApplication app;
    private AssetManager assetManager;
    
    
    public Terrain(World world, Entity owner, SimpleApplication app) {
        super(world, owner, app);
        this.app = app;
        this.bullet = app.getStateManager().getState(BulletAppState.class);
        systemRoot = app.getRootNode();
        assetManager = app.getAssetManager();
        
    }
    
    @Override
    public void initalize() {
       
        // Create the heightmap from a greyscale texture
        AbstractHeightMap heightmap = null;
        Texture heightMapImage = assetManager.loadTexture("Textures/terrain/tech1.png");
        heightmap = new ImageBasedHeightMap(heightMapImage.getImage());
        heightmap.load();

        mat_terrain = new Material(assetManager, "Common/MatDefs/Terrain/Terrain.j3md");

        // Set Alpha texture
        mat_terrain.setTexture("Alpha", assetManager.loadTexture("Textures/terrain/tech1a.png"));
        
        // Set texture 1
        Texture t1 = assetManager.loadTexture("Textures/terrain/8.jpg");
       // t1.setWrap(Texture.WrapMode.Repeat);
        mat_terrain.setTexture("Tex1", t1);
        // Set texture 2
        Texture t2 = assetManager.loadTexture("Textures/terrain/7.png");
        //t2.setWrap(Texture.WrapMode.Repeat);
        mat_terrain.setTexture("Tex2", t2);
        mat_terrain.setFloat("Tex2Scale", 32f);
        
        // Set texture 1
        Texture t3 = assetManager.loadTexture("Textures/terrain/spacestockimage.jpg");
        //t3.setWrap(Texture.WrapMode.Repeat);
        mat_terrain.setTexture("Tex3", t3);
        //mat_terrain.setFloat("Tex3Scale", 128f);
        
        // Apply texture to heightmap
        terrainQuad = new TerrainQuad("islands", 33, 129, heightmap.getHeightMap());
        terrainQuad.setMaterial(mat_terrain);
        
        // Set LOD
        TerrainLodControl control = new TerrainLodControl(terrainQuad, app.getCamera());
        terrainQuad.addControl(control);
        
        CollisionShape terrainShape =
                CollisionShapeFactory.createMeshShape((Node) terrainQuad);
        terrainPhysics = new RigidBodyControl(terrainShape, 0);

        terrainQuad.addControl(terrainPhysics);
        bullet.getPhysicsSpace().add(terrainPhysics);
        terrainPhysics.setPhysicsLocation(Vector3f.ZERO);
        
//          
          
        // Attach to local Node
        systemRoot.attachChild(terrainQuad);  
     //   
    }

    @Override
    public void render() {
//        PhysicsSpace ps = bullet.getPhysicsSpace();
  //      ps.add(terrainPhysics);     
    }
    
}
